﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.Drawing;

using Lottery.MediaData.Core;

namespace Lottery.MediaData.UI.Control {
    class DZXGrid : DataGridView {

        public DZXGrid() {
            this.CellEndEdit += new DataGridViewCellEventHandler(DZXGrid_CellEndEdit);
            this.DataError += new DataGridViewDataErrorEventHandler(DZXGrid_DataError);
        }

        void DZXGrid_DataError(object sender, DataGridViewDataErrorEventArgs e) {
            
        }

        void DZXGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
            var row = e.RowIndex;
            var col = e.ColumnIndex;

            bool isOK = CheckData(row);
            var cell = this[17, row];
            if (!isOK)
                cell.Style.ForeColor = Color.Red;
            else
                cell.Style.ForeColor = Color.Black;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
            var cell = this.CurrentCell;
            if (msg.WParam.ToInt32() == (int)Keys.Return && cell != null) {
                var row = cell.RowIndex;
                var col = cell.ColumnIndex;

                // A: col == 16; B: col == 18
                if (col == 16) {
                    this.CurrentCell = this[col + 2, row];
                    return true;
                } else if (col == 18 && row != this.RowCount - 1) {
                    this.CurrentCell = this[col - 2, row + 1];
                    return true;
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        /// <summary>
        /// 初始化Grid
        /// </summary>
        public void InitGrid() {
            AutoGenerateColumns = false;
            AllowUserToAddRows = false;
            AllowUserToDeleteRows = false;
            AllowUserToOrderColumns = false;
            AllowUserToResizeRows = false;
            AllowUserToResizeColumns = false;

            EditMode = DataGridViewEditMode.EditOnEnter;
            Rows.Clear();
            Columns.Clear();

            var colFirst = new DataGridViewTextBoxColumn();
            colFirst.Visible = true;
            colFirst.ReadOnly = true;
            colFirst.HeaderText = "";
            colFirst.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            colFirst.SortMode = DataGridViewColumnSortMode.NotSortable;
            Columns.Add(colFirst);

            for (int i = 1; i < 15; i++) {
                var col = new DataGridViewTextBoxColumn();
                col.Width = 40;
                col.Visible = true;
                col.ReadOnly = true;
                col.HeaderText = i.ToString();
                col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
                Columns.Add(col);
            }

            // col 15
            var colSum = new DataGridViewTextBoxColumn();
            colSum.Width = 40;
            colSum.Visible = true;
            colSum.ReadOnly = true;
            colSum.HeaderText = "合计";
            colSum.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            colSum.SortMode = DataGridViewColumnSortMode.NotSortable;
            Columns.Add(colSum);

            // col 16
            var colA = new DataGridViewComboBoxColumn();
            colA.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            colA.Width = 60;
            colA.Visible = true;
            colA.ReadOnly = false;
            colA.HeaderText = "A≤";
            colA.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            colA.SortMode = DataGridViewColumnSortMode.NotSortable;
            Columns.Add(colA);

            // col 17
            var colL = new DataGridViewTextBoxColumn();
            colL.Width = 20;
            colL.Visible = true;
            colL.ReadOnly = true;
            colL.HeaderText = "";
            colL.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            colL.SortMode = DataGridViewColumnSortMode.NotSortable;
            Columns.Add(colL);

            // col 18
            var colB = new DataGridViewComboBoxColumn();
            colB.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            colB.Width = 60;
            colB.Visible = true;
            colB.ReadOnly = false;
            colB.HeaderText = "≤B";
            colB.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            colB.SortMode = DataGridViewColumnSortMode.NotSortable;
            Columns.Add(colB);

            // 初始化行
            for (int i = 0; i < 15; i++) {
                Rows.Add();
                this[17, i].Value = "限";
            }

            this.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            // 第一列居左
            for (int i = 0; i < 15; i++) {
                this[0, i].Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            }

            this[0, 0].Value = "大数(场)";
            this[0, 1].Value = "中数(场)";
            this[0, 2].Value = "小数(场)";
            this[0, 3].Value = "与预测号相同";
            this[0, 4].Value = "其中：大数同";
            this[0, 5].Value = "      中数同";
            this[0, 6].Value = "      小数同";
            this[0, 7].Value = "与预测号相邻";
            this[0, 8].Value = "其中：大数邻";
            this[0, 9].Value = "      中数邻";
            this[0, 10].Value = "      小数邻";
            this[0, 11].Value = "与预测号同+邻";
            this[0, 12].Value = "其中：大同+邻";
            this[0, 13].Value = "      中同+邻";
            this[0, 14].Value = "      小同+邻";

            // 序号
            this.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
            for (int i = 0; i < RowCount; i++) {
                var r = Rows[i];
                r.HeaderCell.Value = (i + 1).ToString();
            }

            this.AutoResizeColumns();
        }

        public void SetCellData(CellData data) {
            for (int col = 0; col < data.ColCount; col++) {
                for (int row = 0; row < this.RowCount; row++) {
                    this[col + 1, row].Value = data[col, row];
                }
            }

            for (int row = 0; row < data.RowCount; row++) {
                // colA, colB
                var a = this[16, row] as DataGridViewComboBoxCell;
                var b = this[18, row] as DataGridViewComboBoxCell;
                a.Items.Clear();
                b.Items.Clear();

                int sum = data.GetSum(row);

                for (int iCount = 0; iCount < sum + 1; iCount++) {
                    a.Items.Add(iCount.ToString());
                    b.Items.Add(iCount.ToString());
                }

                var low = data.GetRowLow(row);
                if (string.IsNullOrEmpty(low)) {
                    low = "0";
                }
                a.Value = low;

                var up = data.GetRowUp(row);
                if (string.IsNullOrEmpty(up)) {
                    up = sum.ToString();
                }
                b.Value = up;
            }
        }

        protected virtual string TableKey {
            get { return "3"; }
        }

        public FTCellData GenFtCellData(FTCellData ft, string subIndex) {
            int index = ft.RowCount;
            for (int row = 0; row < this.RowCount; row++) {
                var list = new List<string>();
                list.Add((++index).ToString());
                for (int col = 1; col < 15; col++) {
                    var v = this[col, row].Value;
                    var s = (v == null) ? string.Empty : v.ToString();
                    list.Add(s);
                }

                // 限制范围
                var limit = string.Format("{0}-{1}", this[16, row].Value, this[18, row].Value);
                list.Add(limit);

                // 正确
                list.Add(string.Empty);

                // 限制名称
                if (row == 0 || row == this.RowCount - 1) {
                    list.Add(string.Format("{2} 表{0}-{1}", TableKey, subIndex, row + 1));
                } else {
                    list.Add((row + 1).ToString());
                }

                ft.AddRow(list);
            }
            return ft;
        }

        public void ClearData() {
            for (int row = 0; row < RowCount; row++) {
                for (int col = 1; col < ColumnCount; col++) {
                    if (col == 17) {
                        // 这列是文字 '限'
                        continue;
                    }
                    this[col, row].Value = string.Empty;
                }
            }
        }

        public CellData GetCellData() {
            var result = new CellData();
            for (int col = 0; col < result.ColCount; col++) {
                for (int row = 0; row < result.RowCount; row++) {
                    var v = this[col + 1, row].Value;

                    result[col, row] = v == null ? string.Empty : v.ToString();
                }
            }

            for (int row = 0; row < result.RowCount; row++) {
                var low = this[16, row].Value.ToString();
                var up = this[18, row].Value.ToString();
                result.SetRowLow(row, low);
                result.SetRowUp(row, up);
            }

            return result;
        }

        public bool HasData {
            get {
                // 检查第一行是否有数据
                for (int col = 0; col < 14; col++) {
                    var v = this[col, 1].Value;
                    if (v != null && v.ToString() != string.Empty) {
                        return true;
                    }
                }

                return false;
            }
        }

        /// <summary>
        /// 检查某行的数据是否正确
        /// </summary>
        /// <param name="row">行号</param>
        /// <returns></returns>
        public bool CheckData(int row) {

            var a = this[16, row].Value;
            if (a == null || a.ToString() == string.Empty) {
                return false;
            }

            var b = this[18, row].Value;
            if (b == null || b.ToString() == string.Empty) {
                return false;
            }

            return Convert.ToInt32(a) <= Convert.ToInt32(b);
        }
    }
}
